<template>
  <div id="app">
    <div id="chart-container" ref="chartContainer" style="height: 100%; width: 100%; position: absolute; top: 5%; left: 0;"></div>
  </div>
</template>

<script>
import * as echarts from 'echarts';

export default {
  name: 'EchartsDemo',
  mounted() {
    this.initChart();
    window.addEventListener('resize', this.resizeChart);
  },
  beforeDestroy() {
    window.removeEventListener('resize', this.resizeChart);
  },
  methods: {
    initChart() {
      const chartContainer = this.$refs.chartContainer;
      this.myChart = echarts.init(chartContainer, null, {
        renderer: 'canvas',
        useDirtyRect: false
      });

      const option = {
        tooltip: {
          trigger: 'axis',
          axisPointer: {
            type: 'shadow'
          }
        },
        // legend: {
        //   data: ['数据']
        // },
        xAxis: {
          type: 'category',
          data: ['2022年', '2023年', '2024年']
        },
        yAxis: {
          type: 'value',
          min: 0,
          max: 100000,
          interval: 20000,
          axisLabel: {
            formatter: '{value}'
          }
        },
        series: [
          {
            name: '数据',
            type: 'bar',
            data: [70000, 85000, 90000],
            itemStyle: {
              color: '#4756fd'
            }
          }
        ]
      };

      if (option && typeof option === 'object') {
        this.myChart.setOption(option);
      }
    },
    resizeChart() {
      this.myChart.resize();
    }
  }
};
</script>

<style scoped>
* {
  margin: 0;
  padding: 0;
}
#chart-container {
  position: relative;
  height: 400px;
  overflow: hidden;
}
</style>
